<template>
  <div>
    <el-form :inline="true">
      <el-form-item>
        <select-credit
          v-model="params['service_id.eq']"
          filterable
          @select="selectAgency"
        ></select-credit>
      </el-form-item>
      <el-form-item>
        <c-select
          clearable
          v-model="params['gua_type.eq']"
          @change="getData()"
          :options="{
            4: '省联担',
            2: '直担',
          }"
        ></c-select>
      </el-form-item>
      <el-form-item>
        <c-select
          clearable
          v-model="params['channel_type.eq']"
          @change="getData()"
          :options="{
            1: '便捷',
            0: '常规',
          }"
        ></c-select>
      </el-form-item>
      <el-form-item>
        <c-select
          v-model="params['is_history.eq']"
          clearable
          placeholder="历史数据或新增项目"
          :options="{
            0: '新增项目',
            1: '历史数据',
          }"
          @change="getData()"
        ></c-select>
      </el-form-item>
      <el-form-item>
        <c-select
          v-model="seachParams"
          clearable
          placeholder="项目状态"
          :options="searchOptions"
          @change="setSeachParams"
        ></c-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getData()">查询</el-button>
      </el-form-item>
    </el-form>

    <c-table
      border
      class="table-sm"
      v-loading="loading"
      :columns="tableColumns"
      :data="tableData"
    ></c-table>
  </div>
</template>

<script>
import SelectCredit from '@/cadmin/views/org/components/SelectCredit'
export const searchOptions = [
  {
    label: '未受理',
    params: { 'is_approval.eq': 0 },
    value: 'is_approval=0',
  },
  {
    label: '已受理',
    params: { 'is_approval.eq': 1 },
    value: 'is_approval=1',
  },
  {
    label: '受理拒绝',
    params: { 'is_approval.eq': 2 },
    value: 'is_approval=2',
  },
  {
    label: '未立项',
    params: { 'item_status.eq': 0, 'is_approval.eq': 1 },
    value: 'item_status=0',
  },
  {
    label: '已立项',
    params: { 'item_status.eq': 1, 'is_approval.eq': 1 },
    value: 'item_status=1',
  },
  {
    label: '立项拒绝',
    params: { 'item_status.eq': 2, 'is_approval.eq': 1 },
    value: 'item_status=2',
  },
  {
    label: '调查中',
    params: { 'item_status.eq': 1, 'project_res_status.lt': 2, 'confuse_status.neq': 3 },
    value: 'project_res_status<2',
  },
  {
    label: '调查拒绝',
    params: { 'item_status.eq': 1, 'confuse_status.eq': 3, 'contract_approval_status.eq': 0 },
    value: 'project_res_status=？3',
  },
  {
    label: '合同处理中',
    params: {
      'project_res_status.eq': 2,
      'confuse_status.neq': 3,
      'contract_approval_status.lt': 2,
    },
    value: 'contract_approval_status<2',
  },
  {
    label: '合同拒绝',
    params: {
      'project_res_status.eq': 2,
      'confuse_status.eq': 3,
      'loan_approval_status.lt': 1,
    },
    value: 'contract_approval_status=confuse',
  },
  {
    label: '签约中',
    params: {
      'confuse_status.neq': 3,
      'contract_approval_status.eq': 2,
    },
    value: 'contract_approval_status=2',
  },
  {
    label: '放款审批',
    params: {
      'contract_approval_status.eq': 3,
      'confuse_status.neq': 3,
      'loan_approval_status.lt': 2,
    },
    value: 'loan_approval_status<2',
  },
  {
    label: '放款审批拒绝',
    params: {
      'contract_approval_status.eq': 3,
      'confuse_status.eq': 3,
    },
    value: 'loan_approval_status=confuse',
  },
  {
    label: '还款登记中',
    params: {
      'loan_approval_status.eq': 2,
      'confuse_status.neq': 3,
    },
    value: 'loan_approval_status=2',
  },
  {
    label: '还款登记拒绝',
    params: {
      'loan_approval_status.gte': 2,
      'confuse_status.eq': 3,
    },
    value: 'loan_approval_status=confuse2',
  },
  {
    label: '在保项目',
    params: {
      'loan_approval_status.eq': 3,
      'load_beh_status.neq': 6,
      'confuse_status.neq': 3,
    },
    value: 'loan_approval_status=3',
  },
  {
    label: '解保项目',
    params: {
      'loan_approval_status.eq': 3,
      'load_beh_status.eq': 6,
      'confuse_status.neq': 3,
    },
    value: 'load_beh_status=6',
  },
  {
    label: '已拒绝项目',
    params: {
      'confuse_status.eq': 3,
    },
    value: 'confuse_status=3',
  },
]
export default {
  components: {
    SelectCredit,
  },
  data() {
    return {
      loading: false,
      tableData: [],
      tableColumns: [
        { attrs: { label: '机构名称', prop: 'service_name' } },
        { attrs: { label: '在保项目数', prop: 'p' } },
        { attrs: { label: '在保金额(万元)', prop: 'p1' } },
        { attrs: { label: '申请金额(万元)', prop: 'p2' } },
      ],
      params: {
        'channel_type.eq': '0',
        'gua_type.eq': '4',
        'is_history.eq': '0',
        'service_id.eq': '1a2c693d-9fd3-4a5c-8d8d-df4907d38618',
        groupFunc:
          'count(customer_related.customer_related_id),round(sum(reply_funds)|2),round(sum(apply_funds)|2)',
      },
      serviceName: '铜仁市财农信贷融资担保有限责任公司',
      seachParams: '',
      searchOptions,
    }
  },

  created() {
    this.getData()
  },
  methods: {
    setSeachParams(a) {
      const data = this.searchOptions.find(item => item.value === a) || {}
      const params = data.params || {}
      this.params = {
        ...params,
        'channel_type.eq': this.params['channel_type.eq'],
        'is_history.eq': this.params['is_history.eq'],
        groupFunc: this.params.groupFunc,
        'service_id.eq': this.params['service_id.eq'],
        'gua_type.eq': this.params['gua_type.eq'],
      }
      this.getData()
    },
    selectAgency(select) {
      this.serviceName = select.agency_name
      this.getData()
    },
    getData() {
      if (!this.serviceName) {
        return
      }
      this.loading = true
      this.$api
        .getData('credit', 'customer_related', this.params)
        .then(res => {
          this.tableData = (res.data || []).map(item => {
            item.service_name = this.serviceName
            return item
          })
        })
        .finally(() => {
          this.loading = false
        })
    },
  },
}
</script>
<style lang="scss" scoped></style>
